<?php

namespace app\controller\admin\design;

use app\BaseController;
use app\model\DiyModel;
use think\facade\Request;
use app\model\SettingModel;
use app\model\CategoryModel;
use app\model\BrandModel;
use app\model\GoodsModel;
use app\model\cms\ArticleModel;
use app\model\tool\ImageModel;

class Diy extends BaseController
{


    public function index(Request $request)
    {

        global $_GPC;
        $uniacid = current_uni_account();

        $_GPC = $request::param();

        //todo 获取设为首页的 diy id
        // $diy_home = null;
        
        $diyModel = DiyModel::where( 'uniacid', $uniacid );
        if( !empty( $_GPC['platform'] ) ){
            if($_GPC['platform'] == 'pc' ){
                $diy_home = SettingModel::get2('diy', 'pc_home'); 
            }
            if($_GPC['platform'] == 'xcx' ){
                $diy_home = SettingModel::get2('diy', 'home'); 
            }
            $diyModel->where( 'platform', $_GPC['platform'] );
        }else{
            $diy_home = SettingModel::get2('diy', 'home');  // 默认为小程序DIY页面的首页

        }
        $home_diy_id = !empty($diy_home)? intval($diy_home['value']) : 0;
        
        $list = $diyModel->select();
        
        foreach( $list as &$diy_item ){

            if( $diy_item['id'] == $home_diy_id){

                $diy_item['home_setted'] = 1;
            }else{
                $diy_item['home_setted'] = 0;
            }
            
            
        }

        return json($list);
    }


    public function single(Request $request)
    {

        $diy_id = $request::get('id');

        $diy = DiyModel::where('id', $diy_id)->find();

        $diy->diy = json_decode($diy->diy, true);
        // $diy->diy = json_decode( '', true );
        $diy->diy_parse_error = json_last_error();


        return json($diy);
    }


    public function edit(Request $request)
    {

        $_GPC = $request::param();
        $uniacid = current_uni_account();
        // global $_GPC, $_W;

        $res = [
            'status' => 0
        ];
        $diy_id = 0;
        if (!empty($_GPC['diy_id'])) {
            $diy_id = intval($_GPC['diy_id']);
        }

        if (!empty( $_GPC['__input']) ) {
            $post = $_GPC['__input'];
        } else {
            $post = $_GPC;
        }

        if (empty($post['title'])) {

            $res['description'] = '标题不能为空';
            $res['status'] = 1;
            return json($res);
        }
        if (empty($post['diy'])) {
            $res['description'] = '内容不能为空';
            $res['status'] = 1;
            return json($res);
        }

        $data = [
            'title'         => $post['title'],
            'status'        => !empty($post['status'])?$post['status']:1,
            // 'is_home'       => $post['is_home'],
            'platform'      => !empty($post['platform'])?$post['platform']:'xcx',
            'diy'           => $post['diy'],
            'updated_at'    => date('Y-m-d H:i:s'),
        ];

        if (empty($diy_id)) {
            // $data['uniacid'] = $_W['uniacid'];
            $data['uniacid'] = $uniacid;
            $data['created_at'] = date('Y-m-d H:i:s');
            $res['ret'] = diyModel::create($data);
        } else {
            // $res['ret'] = diyModel::edit($diy_id, $data);
            $res['ret'] = diyModel::update($data, ['id' => $diy_id]);
        }
        return json($res);
    }

    public function set_home(Request $request)
    {

        // global $_GPC;
        $param = $request::param();
        // $uniacid = current_uni_account();
        $res = [
            'status' => 0
        ];

        // if ($_GPC['__input']) {
        //     $param = $_GPC['__input'];
        // } else {
        //     $param = $_GPC;
        // }

        $diy_id = 0;
        if ($param['diy_id']) {
            $diy_id = intval($param['diy_id']);
        }

        if ($diy_id) {

            SettingModel::set2('diy', 'home', $diy_id);
        } else {
            $res['code'] = 1;
            $res['message'] = '请求不合法';
        }

        return json($res);
    }

    /**
     * 适用于pc浏览器端的页面
     */
    public function set_pc_home(Request $request)
    {

        // global $_GPC;
        $param = $request::param();
        // $uniacid = current_uni_account();
        $res = [
            'status' => 0
        ];

        // if ($_GPC['__input']) {
        //     $param = $_GPC['__input'];
        // } else {
        //     $param = $_GPC;
        // }

        $diy_id = 0;
        if ($param['diy_id']) {
            $diy_id = intval($param['diy_id']);
        }

        if ($diy_id) {

            SettingModel::set2('diy', 'pc_home', $diy_id);
        } else {
            $res['code'] = 1;
            $res['message'] = '请求不合法';
        }

        return json($res);
    }

    public function cancel_home(Request $request)
    {

        // global $_GPC;
        $_GPC = $request::param();
        // $uniacid = current_uni_account();
        $res = [
            'status' => 0
        ];

        if ($_GPC['__input']) {
            $param = $_GPC['__input'];
        } else {
            $param = $_GPC;
        }

        $diy_id = 0;
        if ($param['diy_id']) {
            $diy_id = intval($param['diy_id']);
        }

        if ($diy_id) {
            // settingModel::delete('diy', 'home');
            SettingModel::delete2('diy', 'home');
        } else {
            $res['status'] = 1;
            $res['decription'] = '请求不合法';
        }

        return json($res);
    }

    public function delete(Request $request)
    {

        // global $_GPC;
        $_GPC = $request::param();
        $res = [
            'status' => 0
        ];

        $diy_id = 0;
        if ($_GPC['__input']) {
            $param = $_GPC['__input'];
        } else {
            $param = $_GPC;
        }
        if (empty($param['diy_id'])) {
            $res['description'] = '内容不能为空';
            $res['status'] = 1;
            return json($res);
        }

        // $diy_home = settingModel::get('diy', 'home');
        $diy_home = SettingModel::get('diy', 'home');
        $diy_id = intval($param['diy_id']);
        if (!empty($diy_home) && $diy_home['value'] == $diy_id) {
            $res['description'] = '请先取消首页设置';
            $res['status'] = 1;
            return json($res);
        }
        // $res['ret'] = DiyModel::delete($diy_id);
        $res['ret'] = DiyModel::destroy([$diy_id]);
        return json($res);
    }


    public function category(Request $request)
    {

        // global $_GPC;
        $_GPC = $request::param();

        if (isset($_GPC['name'])) {
            $filter['name'] = $_GPC['name'];
        }
        if (isset($_GPC['status'])) {
            $filter['status'] = $_GPC['status'];
        }
        if (isset($_GPC['category_ids'])) {
            $filter['category_ids'] = $_GPC['category_ids'];
        }

        // $list = categoryModel::get_path_list($filter);
        $list = CategoryModel::get_path_list($filter);

        foreach ($list as &$item) {
            // $item['image'] = tomedia($item['image']);
            $item['image'] = ImageModel::resize2( $item['image'] );
        }

        return json($list);
    }

    public function brand(Request $request)
    {

        // global $_GPC;
        $_GPC = $request::param();
        if (isset($_GPC['name'])) {
            $filter['name'] = $_GPC['name'];
        }
        if (isset($_GPC['status'])) {
            $filter['status'] = $_GPC['status'];
        }
        //        if( isset($_GPC['brand_ids']) ){
        //            $filter['brand_ids'] = $_GPC['brand_ids'];
        //        }

        $list = BrandModel::search($filter);

        foreach ($list as &$item) {
            $item['img'] = ImageModel::resize2( $item['img'] );
        }

        return json($list);
    }

    public function product(Request $request)
    {

        // global $_GPC;
        $_GPC = $request::param();
        if (isset($_GPC['name'])) {
            $filter['name'] = $_GPC['name'];
        }

        if (isset($_GPC['status'])) {
            $filter['status'] = $_GPC['status'];
        }

        $list = GoodsModel::get_list($filter);

        foreach ($list as &$item) {
            // $item['image'] = tomedia($item['image']);
            $item['image'] = ImageModel::resize2( $item['image'] );
        }

        return json($list);
    }

    public function article(Request $request)
    {

        // global $_GPC, $_W;
        $_GPC = $request::param();
        if (isset($_GPC['title'])) {
            $filter['title'] = $_GPC['title'];
        }

        if (isset($_GPC['status'])) {
            $filter['status'] = $_GPC['status'];
        }
        $list = [];
        // if (file_exists(IA_ROOT . '/addons/sm_shop_plugin_cms/admin/model/articleModel.php')) {
        //     $select = 'id, title, subtitle, public_time, image';
        //     $list = \sm_shop_plugin_cms\admin\model\articleModel::get_list($filter, $select);
        //     foreach ($list as &$item) {
        //         $item['public_time'] = Date('Y-m-d', strtotime($item['public_time']));
        //     }
        // }
        $select = 'id, title, subtitle, public_time, image';
        $list = ArticleModel::get_list($filter, $select);
        foreach ($list as &$item) {
            $item['public_time'] = Date('Y-m-d', strtotime($item['public_time']));
        }
        return json($list);
    }

        /**
     * 获取自定义页面列表
     */
    public function diy(){
        
        $list = DiyModel::where('uniacid', 8)->where('status',1)->select();

        return json($list);

    }
}
